<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;

use App\Controller\AbstractController;
use App\Model\Tag;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller]
class TagController extends AbstractController
{
    #[GetMapping(path: '/api/tag/list')]
    public function list()
    {
        $tags = Tag::query()
            ->selectRaw('sys_tag.id, sys_tag.name, COUNT(sys_article.id) as articleNum')
            ->leftJoin('article_tag', 'tag.id', '=', 'article_tag.tag_id')
            // ->leftJoin('article', 'article_tag.article_id', '=', 'article.id')
            ->leftJoin('article', function ($join) {
                $join->on('article_tag.article_id', '=', 'article.id')->where('article.status', '=', 1);
            })
            ->groupBy('tag.id', 'tag.name')
            ->get();

        return success($tags);
    }
}
